﻿using J.Status.Application.Command.RoomTypeCommand;
using J.StatusModel;
using J.StatusModel.Dto;
using J.StatusRepository.Impl;
using J.StatusRepository.Interface;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace J.Status.Controllers
{
    /// <summary>
    /// 房型管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoomTypeController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly IRoomTypeRepository roomTypeRepository;
        public readonly IRoomNumManageRepository _IRoomNumManageRepository;
        public RoomTypeController(IMediator mediator, IRoomTypeRepository roomTypeRepository, IRoomNumManageRepository roomTypeRepository1)
        {
            this.mediator = mediator;
            this.roomTypeRepository = roomTypeRepository;
            this._IRoomNumManageRepository = roomTypeRepository1;
        }


        /// <summary>
        /// 房型批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<int> DeleteRangeRoomType(RoomTypeDeleteCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 房型回显
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<RoomTypeModel> FindRoomType(RoomTypeFindCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 返回房型dto
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<RoomTypeDto>> RoomTypeDto()
        {
            return await mediator.Send(new RoomTypeDtoCommand());
        }
        /// <summary>
        /// 返回所有房型列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<RoomTypeModel>> GetAllRoomType()
        {
            return await mediator.Send(new RoomTypeAllCommand());
        }
        /// <summary>
        /// 返回可定房号
        /// </summary>
        /// <param name="roomTypeId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<List<RoomNumManage>> ReturnRoomNum(ReturnRoomNumCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 返回房型列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="roomTypeName"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<IActionResult> GetRoomType(RoomTypeShowCommand command)
        {
            var list= await mediator.Send(command);
            return Ok(new
            {
                list.totalCount,
                list.pageCount,
                list.list
            });
        }
        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<int> AddRoomType(RoomTypeCreateCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 房型修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public async Task<int> UpdateRoomType(RoomTypeUpdateCommand model)
        {
            return await mediator.Send(model);
        }
        /// <summary>
        /// 房间数量计算
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public async Task<int> RoomNumCount(RoomNumCountCommand model)
        {
            return await mediator.Send(model);
        }
        /// <summary>
        /// 商城的房型显示
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RoomNumGEt(int sid) 
        {
            var res = (from x in await roomTypeRepository.GetAllAsync()
                       join a in await _IRoomNumManageRepository.GetAllAsync() on x.ID equals a.RoomId
                       where a.RoomState == RoomState.空房 && a.IsShelf == 1&&x.HotelId== sid
                       select x).Distinct();
                     

            return Ok(res);
        }
        /// <summary>
        /// 房间数量，排房的房号
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RoomNumber(int sid,int roomid)
        {
            var res = (from x in await roomTypeRepository.GetAllAsync()
                       join a in await _IRoomNumManageRepository.GetAllAsync() on x.ID equals a.RoomId
                       where a.RoomState == RoomState.空房 && a.IsShelf == 1 && x.HotelId == sid && a.RoomId == roomid
                       select a);

            var rcount = res.Count();
            return Ok(new { res , rcount });
        }
    }
}
